<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri='http://www.springframework.org/security/tags' prefix='sec'%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../../inc/inc.jsp"></jsp:include>
		<jsp:include page="../auth.jsp"></jsp:include>
		<script type="text/javascript">
			$(document).ready(function () { 
				$("#datagd").datagrid({ 
					url:'info_data', 
			        striped: true, 
			        border: false,
			        collapsible:false,		//是否可折叠的 
			        loadMsg:'正在加载数据...',
			        fit: true,				//自动大小 
			        idField:'id', 
			        singleSelect:true,//是否单选
			        pagination:true,//分页控件 
			        rownumbers:true,//行号 
			        remoteSort:false,
			        columns:[[{
				        field:'roleName',
				        title:'角色名',
				        sortable:true,
				        width:200,
				        halign:'center'
					},{
						field:'roleCode',
						title:'角色code',
						sortable:true,
						width:150,
						halign:'center'
					},{
						field:'remark',
						title:'备注',
						width:300,
						halign:'center'
					}    
					]],
			        toolbar: '#toolbar'
			    });

			});
		</script>
	</head>
	<body>
		<div class="easyui-layout" data-options="fit:true,border:false" >
			<table id="datagd"></table> 
		</div>
		
		<div id="toolbar" style="display:none">
			<sec:authorize ifAnyGranted='B_JSGL_ADD'>
				<a onclick="addRole();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a>
			</sec:authorize>
			<sec:authorize ifAnyGranted='B_JSGL_EDIT'>
				<a onclick="editRole();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">修改</a>
			</sec:authorize>
			<sec:authorize ifAnyGranted='B_JSGL_DEL'>
				<a onclick="destroyRole();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
			</sec:authorize>
			<sec:authorize ifAnyGranted='B_JSGL_AUTH'>
				<a onclick="openAuth();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">授权</a>
			</sec:authorize>
		</div>
		
		<!-- 新增角色dlg -->
		<div id="dlg" class="easyui-dialog" style="width:620px;height:auto;padding:10px 20px" buttons="#dlg-buttons" data-options="closed:true,modal:true">
			<form id="fm" method="post">
				<input type="hidden" name="id" />
				<table  class="grid">
					<tr>
						<td>名称</td>
						<td>
							<input id="roleName" name="roleName" type="text" class="easyui-textbox" data-options="required:true,validType:['length[0,40]']" />
						</td>
					</tr>
					<tr>
						<td>角色code</td>
						<td>
							<input id="roleCode" name="roleCode" type="text" class="easyui-textbox" data-options="required:true,validType:['length[0,18]']" />
						</td>
					</tr>
					<tr>
						<td>备注</td>
						<td><input type="text" name="remark" class="easyui-textbox" data-options="multiline:true,height:50" style="width:300px;" /></td>
					</tr>
				</table>
			</form>
		</div>
		<div id="dlg-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton c8" iconCls="icon-ok" onclick="saveRole();" style="width:90px">保存</a>
			<a href="javascript:void(0)" class="easyui-linkbutton c2" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
		</div>
	</body>
</html>

<script type="text/javascript">
	//===============================角色增删改=====================================
		
	function saveRole(){
	    var data = form2Json("fm");
	    if($("#fm").form('validate')){
	        $.post('save_update',{data:data},function(result) {
	            if(result.success){
	                $.messager.show({
	                    title: '提示',
	                    msg: result.msg
	                });
	                $('#dlg').dialog('close');
	                $('#datagd').datagrid('reload');
	            }else{
	                $.messager.show({
	                    title: '提示',
	                    msg: result.msg
	                });
	            }
	        },'json');
	    }
	}
	
	function addRole(){
		$('#dlg').dialog('open').dialog('setTitle','增加角色');
		$('#fm').form('clear');
	}
	function editRole(){
		var row = $('#datagd').datagrid('getSelected');
		if (row){
			$('#dlg').dialog('open').dialog('setTitle','修改角色');
			$('#fm').form('load',row);
		}else{
			$.messager.alert('提示','请选择一条记录','info');
		}
	}
	function destroyRole() {
		var row = $('#datagd').datagrid('getSelected');
		if(row){
			$.messager.confirm('提示','确定删除?',function(r){
				if (r){
					$.ajax({
						url:'delete',
						type:'POST',
						data:{id:row.id},
						dataType:'json',
						success: function(result){
		                	if(result.success) {
								$.messager.show({
									title: '提示',
									msg: result.msg
								});
								$('#datagd').datagrid('reload');
								//删除完后easyui还存在数据缓存，并且是选中的，所以取消选中
								$('#datagd').datagrid('clearSelections');
		                	} else {
		                		$.messager.show({
									title: '提示',
									msg: result.msg
								});
		                	}
						}
					});
				}
			});
		} else {
			$.messager.alert('提示','请选择一条记录','info');
		}
	}
	

	//----------------------------------------------------------=========================用户分配=====================================
	function openUser(){
		var row = $('#datagd').datagrid('getSelected');
		if(row){
			$('#userDlg').dialog('open').dialog('setTitle','用户分配');
			$('#selectTree').tree({    
				url:'role/role_select.do?id='+row.id,
				lines:true,
				parentField : 'parentId',
				checkbox : true
			});
			$('#selectedDg').datagrid({ 
				url:'role/role_selected.do?id='+row.id, 
		        iconCls:'icon-edit',//图标 
		        width: 200, 
		        height: 'auto', 
		        border: false,
		        loadMsg:'正在加载数据...',
		        fit: true,				//自动大小 
		        idField:'id', 
		        rownumbers:true,//行号 
		        frozenColumns:[[ 
					{field:'ck',checkbox:true} 
				]], 
		        columns:[[{
			        field:'staff_name',
			        title:'员工姓名',
			        width:150,
			        align:'center'
				}   
		        ]],
		    });
		}else{
			$.messager.alert('提示','请选择一条记录');
		}
	}
	//增加用户保存
	function addUser(){
		var row = $('#datagd').datagrid('getSelected');
		var checknodes = $('#selectTree').tree('getChecked');
		var ids = "";
		for(var i = 0; i < checknodes.length; i++) {
			var id = checknodes[i].id;
			if(checknodes[i].attributes == 'user'){
				ids = ids + id + "#";
			}
		}
		if(ids != ""){
			$.ajax({
				url:'role/role_user_add.do',
				type:'POST',
				data:{role_id:row.id,user_ids:ids},
				success: function(result){
					result = $.parseJSON(result);
                	if(result.success) {
                		$.messager.show({
							title: '提示',
							msg: result.msg
						});
                		$('#selectTree').tree('reload');
    					$('#selectedDg').datagrid('reload');
    					$('#selectedDg').datagrid('clearSelections');
                	} else {
                		$.messager.show({
    						title: '提示',
    						msg: result.msg
    					});
                	}
				}
			});
		}else{
			$.messager.alert('提示','没有选取添加的人员');
		}
	}
	//移除用户
	function removeUser(){
		var row = $('#datagd').datagrid('getSelected');
		var checknodes = $('#selectedDg').datagrid('getChecked');
		var ids = "";
		if (checknodes && checknodes.length > 0) {
			for ( var i = 0; i < checknodes.length; i++) {
				var id = checknodes[i].id;
				ids = ids+id+"#";
			}
			$.ajax({
				url:'role/role_user_remove.do',
				type:'POST',
				data:{role_id:row.id,user_ids:ids},
				success: function(result){
					result = $.parseJSON(result);
                	if(result.msg == '1') {
                		$.messager.show({
							title: '提示',
							msg: result.msg
						});
						$('#selectTree').tree('reload');
						$('#selectedDg').datagrid('reload');
						$('#selectedDg').datagrid('clearSelections');
                	} else {
                		$.messager.show({
    						title: '提示',
    						msg: result.msg
    					});
                	}
				}
			});
		}else{
			$.messager.alert('提示','请选择移除的人员');
		}
	}
</script>

	
